package com.TechMahindra.wx.utils.weixin;

import com.TechMahindra.wx.common.QywxPropertieKeys;
import com.TechMahindra.wx.entity.response.message.SendMessageResultMsg;
import com.TechMahindra.wx.service.impl.AccessTokenService;
import com.TechMahindra.wx.utils.HttpClientUtil;
import com.TechMahindra.wx.utils.PropertiesUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;

/**
 * 审批测试处理类
 * @author weican
 */
public class ApprovalTestService {
	
	private static Logger log = LoggerFactory.getLogger(ApprovalTestService.class);
	
	
	public static void main(String[] args) {
		List<String> userIds = new ArrayList<>();
		userIds.add("WeiCan");
		
//		sendText(userIds, Agentids.approvalTestAid, "审批应用：测试text类型信息", 0);
		
		sendTextcard(userIds, Integer.parseInt(PropertiesUtil.getValue(QywxPropertieKeys.approvalTest_agentId)), "审批通知", "<div class=\\\"highlight\\\">审批通知测试消息</div>", "http://test.t.com");
		
//		System.out.println(StringUtils.capitalize("<div class=\"highlight\">审批通知测试消息</div>"));
	}
	
	
	/**
	 * 发送文本类型消息
	 * @param userIds
	 * @param agentid
	 * @param content
	 * @param safe
	 * @return
	 */
	public static SendMessageResultMsg sendText(List<String> userIds, int agentid, String content, Integer safe){
		String accessToken =  AccessTokenService.getAccessToken(PropertiesUtil.getValue(QywxPropertieKeys.approvalTest_secret));
		
		String params = getParamForText(userIds, MessageType.text.toString(), agentid, content, safe);
		String resultMsg = HttpClientUtil.getInstance().sendHttpPost(String.format(PropertiesUtil.getValue(QywxPropertieKeys.message_send), accessToken), params);
		SendMessageResultMsg smrm = JSON.parseObject(resultMsg, SendMessageResultMsg.class);
		return smrm;
	}
	
	
	/**
	 * 获取发送文本类型参数
	 * @param userIds 用户ID
	 * @param agentid 企业应用的id
	 * @param content 发送内容最长2048个字节
	 * @param safe 表示是否是保密消息，0表示否，1表示是，默认0
	 */
	public static String getParamForText(List<String> userIds, String type, int agentid,
			String content, Integer safe) {
		StringBuffer param = getParamHead(userIds, type, agentid);
		param.append("\"text\" : {\"content\" : \"").append(content).append("\", },");
		param.append("\"safe\": " + (safe == null ? 0 : safe));
		param.append("}");

		return param.toString();
	}
	
	
	/**
	 * 文本卡片消息发送
	 * @param userIds
	 * @param agentid
	 * @param title
	 * @param description
	 * @param url
	 * @return
	 */
	public static SendMessageResultMsg sendTextcard(List<String> userIds, int agentid, String title, String description, String url){
		String accessToken =  AccessTokenService.getAccessToken(PropertiesUtil.getValue(QywxPropertieKeys.approvalTest_secret));
		
		String params = getParamForTextcard(userIds, MessageType.textcard.toString(), agentid, title, description, url);
		String resultMsg = HttpClientUtil.getInstance().sendHttpPost(String.format(PropertiesUtil.getValue(QywxPropertieKeys.message_send), accessToken), params);
		SendMessageResultMsg smrm = JSON.parseObject(resultMsg, SendMessageResultMsg.class);
		log.info("params === "+params);
		log.info("resultMsg === "+resultMsg);
		return smrm;
	}
	
	public static String getParamForTextcard(List<String> userIds, String type, int agentid, String title, String description, String url){
		StringBuffer param = getParamHead(userIds, type, agentid);
		param.append("\"textcard\" : {\"title\" : \"").append(title).append("\",");
		param.append("\"description\" : \"").append(description).append("\",");
		param.append("\"url\": \"").append(url).append("\" }");
		param.append("}");

		return param.toString();
	}
	


	/**
	 * 拼接请求参数公共部分
	 * @param userIds 用户ID
	 * @param type 类型
	 * @param agentid 企业应用的id
	 * @return
	 */
	private static StringBuffer getParamHead(List<String> userIds, String type,
			int agentid) {
		StringBuffer param = new StringBuffer("{\"touser\" : ");

		if (userIds == null) {
			param.append("\"@all\",");
		} else {
			param.append("\"");
			for (String userId : userIds) {
				param.append(userId + "|");
			}
			param.delete(param.length() - 1, param.length());
			param.append("\",");
		}
		//部门和标签暂不实现
		param.append("\"toparty\" : \"\", ").append("\"totag\" : \"\", ");
		
		param.append("\"msgtype\" : \"").append(type).append("\",");
		param.append("\"agentid\" : "+ agentid +",");
		return param;
	}
	
	
	
	/**
	 * 发送消息类型枚举
	 */
	private enum MessageType {
		//文本
		text,
		//图片
		image,
		//语音
		voice,
		//语音
		video,
		//文件
		file,
		//文本卡
		textcard,
		//图文
		news,
		//图文(mpnews)
		mpnews;

		@Override
		public String toString() {
			return super.toString();
		}
	}
}
